import React from 'react';
import CloneElement from "@/components/CloneElement";
import {useUserStore} from "@/store/user";

type Props = {
    children: React.ReactElement<any, string | React.JSXElementConstructor<any>>,
    authority: string[]
}
const AuthButton: React.FC<Props> = ({children, authority}) => {
    const {authButtonList} = useUserStore()

    const all_permission = "*:*:*";

    if (authority && authority instanceof Array && authority.length > 0) {
        const hasPermission = authButtonList.some((permission: string) => {
            return all_permission === permission || authority.includes(permission)
        })
        // return <React.Fragment>{hasPermission && children}</React.Fragment>
        //默认禁用antd Button组件，如果想要移除Button请解注上面这行代码
        return <CloneElement element={children} disabled={!hasPermission}/>
    }
};

export default React.memo(AuthButton);
